/** New Car Creation Window **/

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.plaf.basic.*;
import javax.swing.border.*;
import javax.imageio.*;
import javax.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;


public class NewCarMaker{
  JPanel mainPanel, textPanel, submitPanel, comboPanel;
  JFrame mainWindow, superWindow;
  Container thePane;
  SubmitListener theListener;
  JTextField make, model, year, vin;
  JLabel makeL, modelL, yearL, vinL;
  JButton submit;
  Car newCar;
  User currUser;
  
  /** DB Variables- These variables are used to set up a connection with and communicate with our system's database. This data base holds tables for User Profiles, 
      Car Profiles, and Vehicle Statistics. **/
  public Connection dbConnection = null;
 // public String connectString = "jdbc:mysql://ec2-184-73-248-31.compute-1.amazonaws.com:3419/AutoBiography";
 // public String driver = "com.mysql.jdbc.Driver";
 // public String userid = "Team_AutoLytics";
 // public String pwd = "COE1186";
  public Statement stmnt = null;
  public ResultSet r = null;
    public CarSelector parent;
  public NewCarMaker(JFrame s, User curr, Connection dconnection, CarSelector parentSelector){
    dbConnection = dconnection;
    parent = parentSelector;
    superWindow = s;
    currUser = curr;
    
    bluereader blueR = new bluereader("test", dbConnection, null);
    String[] makeModelYr = blueR.getMMY();
    
    mainWindow = new JFrame("AutoBiography");

    thePane = mainWindow.getContentPane();
    
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(4,2));
    
    makeL = new JLabel("Make: ");
    modelL = new JLabel("Model: ");
    yearL = new JLabel("Year: ");
    vinL = new JLabel("VIN: ");

    make = new JTextField();
    make.setPreferredSize(new Dimension(300, 20));
    model = new JTextField();
    year = new JTextField();
    vin = new JTextField();

    make.setText(makeModelYr[0]);
    model.setText(makeModelYr[1]);
    year.setText(makeModelYr[2]);
    vin.setText(makeModelYr[3]);

    textPanel.add(makeL);
    textPanel.add(make);

    textPanel.add(modelL);
    textPanel.add(model);

    textPanel.add(yearL);
    textPanel.add(year);

    textPanel.add(vinL);
    textPanel.add(vin);

    theListener = new SubmitListener();

    submit = new JButton("Submit");
    submit.addActionListener(theListener);
    
    c.gridx = 0;
    c.gridy = 0;
    c.ipady = 20;
    mainPanel.add(textPanel, c);
    c.gridy = 1;
    c.ipady = 0;
    mainPanel.add(submit, c);
    
    thePane.add(mainPanel);
    mainWindow.setSize(1200, 800);
    mainWindow.setVisible(true);
    
  }
  private class SubmitListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == submit){
        if(make.getText().length() == 0){
          JOptionPane.showMessageDialog(null, "Please enter the make of your car!");
        }
        else if(model.getText().length() == 0){
          JOptionPane.showMessageDialog(null, "Please enter the model of your car!");
        }
        else if(year.getText().length() == 0){
          JOptionPane.showMessageDialog(null, "Please enter the year of your car!");
        }
        else if(vin.getText().length() == 0){
          JOptionPane.showMessageDialog(null, "Please enter the VIN of your car!");
        }
        else if(year.getText().length() != 4){
          JOptionPane.showMessageDialog(null, "Please enter a 4-digit value for the year.");
        }
        else if(vin.getText().length() != 17){
          JOptionPane.showMessageDialog(null, "Please enter a 17-character value for the VIN.");
        }
        else{
          // Establishing a connection with our online database.
          try{

            stmnt =  dbConnection.createStatement();
          }catch(Exception e1){
            System.out.println("Connection Error.");
            System.out.println(e);
          }
          try{
            stmnt.executeUpdate("INSERT INTO CARS (MAKE, MODEL, YEAR, VIN, USER, CH_ID) VALUES('"+make.getText()+"', '"+model.getText()+"', '"+year.getText()+"', '"+vin.getText()+"', '"+currUser.username+"', '"+currUser.getCHID()+"')");
              //choice = 8

            parent.popResults();
              
              Car tempcar = new Car(make.getText(), model.getText(), vin.getText(), currUser.username, year.getText());
              parent.addtoCars(tempcar);
              stmnt.executeUpdate("UPDATE USERS SET NUMVEHICLES = NUMVEHICLES+1 WHERE UID = '" + currUser.username + "'");
              //choice = 9

           // new AutoBiographyGUI(currUser, new Car(make.getText(), model.getText(), vin.getText(), currUser.username, year.getText()), dbConnection);  
              
          }catch(SQLException e0){
            System.out.println("Error adding New Car to DB.");
            System.out.println(e0);
          }
          mainWindow.setVisible(false);
          superWindow.setVisible(true);
          return;
        } 
      }
      
    }
  }
}